package com.gssm.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil {
    public static void sendMail(String to, String code, Properties mailProps) throws Exception {
        String host = mailProps.getProperty("mail.host");
        String username = mailProps.getProperty("mail.username");
        String password = mailProps.getProperty("mail.password");
        String port = mailProps.getProperty("mail.port");

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("您的验证码");
        message.setText("您的验证码是：" + code + "，5分钟内有效。");

        Transport.send(message);
    }
} 